package com.eight.clientcourse.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.clientcourse.dao.*;
import com.eight.clientcourse.service.ClientCrouseService;
import com.eight.clientcourse.vo.CourseVo;
import com.eight.clientcourse.vo.GuanZhuVo;
import com.eight.common.pojo.*;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @program: qy8-education1
 * @description:
 * @author: 张亚祥
 * @create: 2023-06-02 16:25
 **/
@Service
public class ClientCrouseServiceImpl implements ClientCrouseService {
    @Resource
    private ClientCourseDao courseDao;
    @Resource
    private ClientSubjectDao subjectDao;
    @Resource
    private ShouCangCourseDao shouCangCourseDao;
    @Resource
    private TbMemberDao tbMemberDao;
    @Resource
    private PlayHistoryDao playHistoryDao;
    @Override
    public List<EduSubject> findCourse() {
        QueryWrapper<EduSubject> wrapper =new QueryWrapper<>();
        wrapper.eq("status_id",'1');
        wrapper.eq("parent_id","0");
        List<EduSubject> EduSubject = subjectDao.selectList(wrapper);
        return EduSubject;
    }
    @Override
    public Page<EduCourse> findPage(Integer currentSize, Integer pageSize, CourseVo courseVo) {
        Page<EduCourse> page = new Page<>(currentSize, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status","Normal");
        if(courseVo.getIsFee ()!=null && !Objects.equals(courseVo.getIsFee(),"")){
            if (courseVo.getIsFee().equals("0")){
            wrapper.eq("price", 0) ;
            }
            if (courseVo.getIsFee().equals("2")){
                wrapper.gt("price", 0) ;
            }
        }
        if(StringUtils.hasText(courseVo.getOneSubjectId())) {
            wrapper.eq("subject_parent_id", courseVo.getOneSubjectId());
        }
        if(StringUtils.hasText(courseVo.getTwoSubjectId())) {
            wrapper.eq("subject_id", courseVo.getTwoSubjectId());
        }
        Page<EduCourse> cherckCourse = courseDao.selectPage(page, wrapper);
        return cherckCourse;
    }

    @Override
    public List<EduVideo> findVideo(String id) {
        List<EduVideo> video = courseDao.findVideo(id);
        return video;
    }

    @Override
    public List<EduCourse> findTeacherByMobile(String id,String mobile) {
        List<EduCourse> teacherByMobile = courseDao.findTeacherByMobile(id,mobile);
        return teacherByMobile;
    }

    @Override
    public EduCourse queryAllCourseAndTeacherName(String id) {
        EduCourse eduCourses = courseDao.queryAllCourseAndTeacherName(id);
        return eduCourses;
    }

    @Override
    public TbMember queryMemberStatus(String mobile) {
        TbMember queryMemberStatus = tbMemberDao.queryMemberStatus(mobile);
        return queryMemberStatus;
    }

    @Override
    public EduCourse findFavoriteCourse(String courseId, String memberId) {
        EduCourse favoriteCourse = shouCangCourseDao.findFavoriteCourse(courseId, memberId);
        return favoriteCourse;
    }

    @Override
    public int addFavoritescourse(TbShoucangCourse tbShoucangCourse) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        List<EduCourse> eduCourses = shouCangCourseDao.addByTeacherStatus(wrapper);
        if(eduCourses!=null){
            wrapper.eq("status","Normal");
            tbShoucangCourse.setId(IdUtil.getSnowflakeNextIdStr());
            tbShoucangCourse.setMemberId(tbShoucangCourse.getMemberId());
            tbShoucangCourse.setCourseId(tbShoucangCourse.getCourseId());
            shouCangCourseDao.insert(tbShoucangCourse);
        }
        return 0;
    }

    @Override
    public int deleteFavoriteCourse(TbShoucangCourse tbShoucangCourse) {
            QueryWrapper<TbShoucangCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id",tbShoucangCourse.getCourseId());
            wrapper.eq("member_id",tbShoucangCourse.getMemberId());
            int delete = shouCangCourseDao.delete(wrapper);
            return 0;

    }

    @Override
    public int insertPlayHistoryByCourseId(String HuiYuanId, String videoId) {

        TbPlayHistory tbPlayHistory=new TbPlayHistory();
        tbPlayHistory.setPlayId(IdUtil.getSnowflakeNextIdStr());
        tbPlayHistory.setUserId(HuiYuanId);
        tbPlayHistory.setVideoId(videoId);
        tbPlayHistory.setPlayTimePoint(LocalDateTime.now());
        playHistoryDao.insert(tbPlayHistory);
        return 0;
    }

}
